{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Diagnostic Function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "A power flow calculation on a pandapower network can fail to converge (or fail to run at all) for a vast variety of reasons, which often makes debugging difficult, annoying and time consuming. To help with that, the diagnostic function automatically checks pandapower networks for the most common issues leading to errors. It provides logging output and diagnoses with a controllable level of detail. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example: faulty network\n",
    "\n",
    "To demonstrate the usage of the diagnostic function we will use a very basic exemplary pandapower network with several flaws.\n",
    "\n",
    "There will be no further explenation on how to create pandapower networks since there's a separate tutorial available in this regard."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "# imports the pandapower module\n",
    "import pandapower as pp\n",
    "\n",
    "# defines a function that creates an example network, which will be used a lot in this tutorial\n",
    "# run this code first in order for the examples to work\n",
    "\n",
    "def faulty_example_network():\n",
    "\n",
    "    net = pp.create_empty_network()\n",
    "\n",
    "    pp.create_bus(net, name = \"110 kV bar\", vn_kv = 110, type = 'b', in_service = 'True')\n",
    "    pp.create_bus(net, name = \"20 kV bar\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 2\", vn_kv = 30, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 3\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 4\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 5\", vn_kv = -20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 6\", vn_kv = 20, type = 'b')\n",
    "    \n",
    "    pp.create_ext_grid(net, 0, vm_pu = 1)\n",
    "\n",
    "    pp.create_line(net, name = \"line 0\", from_bus = 1, to_bus = 2, length_km = 0, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 1\", from_bus = 2, to_bus = 3, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 2\", from_bus = 3, to_bus = 4, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 3\", from_bus = 4, to_bus = 5, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 4\", from_bus = 5, to_bus = 6, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 5\", from_bus = 6, to_bus = 1, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "\n",
    "    pp.create_transformer_from_parameters(net, hv_bus = 1, lv_bus = 0, i0_percent= 0.038, pfe_kw = 11.6,\n",
    "                                          vkr_percent = 0.322, sn_mva = 40.0, vn_lv_kv = 22.0,\n",
    "                                          vn_hv_kv = 110.0, vk_percent = 17.8)\n",
    "\n",
    "    pp.create_load(net, 2, p_mw = -1, q_mvar = 0.200, name = \"load 0\")\n",
    "    pp.create_load(net, 3, p_mw = 1, q_mvar = 0.200, name = \"load 1\")\n",
    "    pp.create_load(net, 4, p_mw = 1, q_mvar = 0.200, name = \"load 2\")\n",
    "    pp.create_load(net, 5, p_mw = 1, q_mvar = 0.200, name = \"load 3\")\n",
    "    pp.create_load(net, 6, p_mw = 1, q_mvar = 0.200, name = \"load 4\")\n",
    "\n",
    "    pp.create_switch(net, bus = 1, element = 0, et = 'l')\n",
    "    pp.create_switch(net, bus = 2, element = 0, et = 'l')\n",
    "    pp.create_switch(net, bus = 2, element = 1, et = 'l')\n",
    "    pp.create_switch(net, bus = 3, element = 1, et = 'l')\n",
    "    pp.create_switch(net, bus = 3, element = 2, et = 'l')\n",
    "    pp.create_switch(net, bus = 4, element = 2, et = 'l')\n",
    "    pp.create_switch(net, bus = 4, element = 3, et = 'l', closed = False)\n",
    "    pp.create_switch(net, bus = 5, element = 3, et = 'l')\n",
    "    pp.create_switch(net, bus = 5, element = 4, et = 'l', closed = False)\n",
    "    pp.create_switch(net, bus = 6, element = 4, et = 'l', closed = False)\n",
    "    pp.create_switch(net, bus = 6, element = 5, et = 'l')\n",
    "    pp.create_switch(net, bus = 1, element = 5, et = 'l')\n",
    "    \n",
    "    return net"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Now let's create the network and try to run a load flow calculation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "LoadflowNotConverged",
     "evalue": "Power Flow nr did not converge after 10 iterations!",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mLoadflowNotConverged\u001b[0m                      Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-25e704087033>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# creates the example network\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mfaulty_net\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfaulty_example_network\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mpp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrunpp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfaulty_net\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\git_repos\\pandapower\\pandapower\\run.py\u001b[0m in \u001b[0;36mrunpp\u001b[1;34m(net, algorithm, calculate_voltage_angles, init, max_iteration, tolerance_mva, trafo_model, trafo_loading, enforce_q_lims, check_connectivity, voltage_depend_loads, consider_line_temperature, **kwargs)\u001b[0m\n\u001b[0;32m    210\u001b[0m     \u001b[0m_check_bus_index_and_print_warning_if_high\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    211\u001b[0m     \u001b[0m_check_gen_index_and_print_warning_if_high\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 212\u001b[1;33m     \u001b[0m_powerflow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    213\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    214\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\git_repos\\pandapower\\pandapower\\powerflow.py\u001b[0m in \u001b[0;36m_powerflow\u001b[1;34m(net, **kwargs)\u001b[0m\n\u001b[0;32m     80\u001b[0m         \u001b[0m_clean_up\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mres\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     81\u001b[0m         raise LoadflowNotConverged(\"Power Flow {0} did not converge after \"\n\u001b[1;32m---> 82\u001b[1;33m                                    \"{1} iterations!\".format(algorithm, max_iteration))\n\u001b[0m\u001b[0;32m     83\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     84\u001b[0m         \u001b[0mnet\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"_ppc\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mLoadflowNotConverged\u001b[0m: Power Flow nr did not converge after 10 iterations!"
     ]
    }
   ],
   "source": [
    "# creates the example network\n",
    "faulty_net = faulty_example_network()\n",
    "pp.runpp(faulty_net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "As you can see there is at least one error in our network that prevents the load flow calculation from running properly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Instead of analysing the code ourselves, we can use the diagnostic function to find the errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "\n",
      "_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: Checking for missing bus indices...\n",
      "hp.pandapower.diagnostic_reports - INFO: \n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No missing bus indices found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for elements without a connection to an external grid...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Disconnected section found, consisting of the following elements:\n",
      "hp.pandapower.diagnostic_reports - WARNING: buses: [5]\n",
      "hp.pandapower.diagnostic_reports - WARNING: switches: [7, 8]\n",
      "hp.pandapower.diagnostic_reports - WARNING: lines: [3]\n",
      "hp.pandapower.diagnostic_reports - WARNING: loads: [3]\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 5 disconnected element(s) found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for connections of different voltage levels...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0 connects bus 1: 20 kV bar (vn_kv = 20.0) and bus 2: bus 2 (vn_kv = 30.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 1 connects bus 2: bus 2 (vn_kv = 30.0) and bus 3: bus 3 (vn_kv = 20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 3 connects bus 4: bus 4 (vn_kv = 20.0) and bus 5: bus 5 (vn_kv = -20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 4 connects bus 5: bus 5 (vn_kv = -20.0) and bus 6: bus 6 (vn_kv = 20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 4 element(s) that connect different voltage levels found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for impedance values close to zero...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0: r_ohm <= 0.001 or x_ohm <= 0.001\n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge after replacing implausible elements with switches.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 element(s) with impedance values close to zero found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for components with deviating nominal voltages...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Trafo(s) [0]: hv and lv connectors seem to be swapped\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 component(s) with deviating nominal voltages found\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for invalid_values...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: bus:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Invalid value found: 'bus 5' with attribute 'vn_kv' = -20.0 (data type: <class 'numpy.float64'>). Valid input needs to be >0.\n",
      "hp.pandapower.diagnostic_reports - WARNING: line:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Invalid value found: 'line 0' with attribute 'length_km' = 0.0 (data type: <class 'numpy.float64'>). Valid input needs to be >0.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 2 invalid values found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for overload...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Overload check failed: Power flow still does not converge with load and generation scaled down to 0.1 percent.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No buses with multiple gens and/or ext_grids found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking switch configuration...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge with all switches closed.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: External grid found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for usage of wrong reference system...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Found load 0: 'load 0' with p_mw = -1.0. In load reference system p_mw should be positive.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: Found 1 load(s) with negative p_mw. In load reference system, p_mw should be positive. If the intention was to model a constant generation, please use an sgen instead.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No elements with deviations from std_type found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: numba_comparison failed due to the following error:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Power Flow nr did not converge after 10 iterations!\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No parallel switches found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: _____________ END OF PANDAPOWER DIAGNOSTIC _____________ \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'disconnected_elements': [{'buses': [5],\n",
       "   'switches': [7, 8],\n",
       "   'lines': [3],\n",
       "   'loads': [3]}],\n",
       " 'different_voltage_levels_connected': {'lines': [0, 1, 3, 4]},\n",
       " 'impedance_values_close_to_zero': [{'line': [0]},\n",
       "  {'loadflow_converges_with_switch_replacement': False}],\n",
       " 'nominal_voltages_dont_match': {'trafo': {'hv_lv_swapped': [0]}},\n",
       " 'invalid_values': {'bus': [(5, 'vn_kv', -20.0, '>0')],\n",
       "  'line': [(0, 'length_km', 0.0, '>0')]},\n",
       " 'overload': {'load': False, 'generation': False},\n",
       " 'wrong_switch_configuration': False,\n",
       " 'wrong_reference_system': {'loads': [0]}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# diagnoses the faulty network\n",
    "pp.diagnostic(faulty_net)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "You can scroll through the report to see all checks that have been performed and their results. In default mode, the report always looks like this:\n",
    "\n",
    "_____________ PANDAPOWER DIAGNOSTIC TOOL _____________\n",
    "\n",
    "Checking for [description of check 1]...\n",
    "\n",
    "    [detailed error description]\n",
    "    \n",
    "SUMMARY: [summary of errors found]\n",
    "\n",
    " --------\n",
    " \n",
    " Checking for [description of the check 2]...\n",
    "\n",
    "    [detailed error description]\n",
    "    \n",
    "SUMMARY: [summary of errors found]\n",
    "\n",
    " --------\n",
    " \n",
    "_____________ END OF PANDAPOWER DIAGNOSTIC _____________ \n",
    "\n",
    "\n",
    "The function also return a dict that contains the indices of all elements where errors were found for further usage."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Customizing the report\n",
    "\n",
    "For a more compact report, there a two options that can be passed as arguments to the diagnostic function:\n",
    "\n",
    "- warnings_only = True : only positive check results (errors found) will be in the report\n",
    "\n",
    "- detailed_report = False: only error summaries, no detailed error descriptions\n",
    "\n",
    "\n",
    "The default setting is:\n",
    "\n",
    "*diagnostic(net, warnings_only = False, detailed_report = True)*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "\n",
      "_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for elements without a connection to an external grid...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Disconnected section found, consisting of the following elements:\n",
      "hp.pandapower.diagnostic_reports - WARNING: buses: [5]\n",
      "hp.pandapower.diagnostic_reports - WARNING: switches: [7, 8]\n",
      "hp.pandapower.diagnostic_reports - WARNING: lines: [3]\n",
      "hp.pandapower.diagnostic_reports - WARNING: loads: [3]\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 5 disconnected element(s) found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for connections of different voltage levels...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0 connects bus 1: 20 kV bar (vn_kv = 20.0) and bus 2: bus 2 (vn_kv = 30.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 1 connects bus 2: bus 2 (vn_kv = 30.0) and bus 3: bus 3 (vn_kv = 20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 3 connects bus 4: bus 4 (vn_kv = 20.0) and bus 5: bus 5 (vn_kv = -20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 4 connects bus 5: bus 5 (vn_kv = -20.0) and bus 6: bus 6 (vn_kv = 20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 4 element(s) that connect different voltage levels found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for impedance values close to zero...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0: r_ohm <= 0.001 or x_ohm <= 0.001\n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge after replacing implausible elements with switches.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 element(s) with impedance values close to zero found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for components with deviating nominal voltages...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Trafo(s) [0]: hv and lv connectors seem to be swapped\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 component(s) with deviating nominal voltages found\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for invalid_values...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: bus:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Invalid value found: 'bus 5' with attribute 'vn_kv' = -20.0 (data type: <class 'numpy.float64'>). Valid input needs to be >0.\n",
      "hp.pandapower.diagnostic_reports - WARNING: line:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Invalid value found: 'line 0' with attribute 'length_km' = 0.0 (data type: <class 'numpy.float64'>). Valid input needs to be >0.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 2 invalid values found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for overload...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Overload check failed: Power flow still does not converge with load and generation scaled down to 0.1 percent.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking switch configuration...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge with all switches closed.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for usage of wrong reference system...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Found load 0: 'load 0' with p_mw = -1.0. In load reference system p_mw should be positive.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: Found 1 load(s) with negative p_mw. In load reference system, p_mw should be positive. If the intention was to model a constant generation, please use an sgen instead.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: _____________ END OF PANDAPOWER DIAGNOSTIC _____________ \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'disconnected_elements': [{'buses': [5],\n",
       "   'switches': [7, 8],\n",
       "   'lines': [3],\n",
       "   'loads': [3]}],\n",
       " 'different_voltage_levels_connected': {'lines': [0, 1, 3, 4]},\n",
       " 'impedance_values_close_to_zero': [{'line': [0]},\n",
       "  {'loadflow_converges_with_switch_replacement': False}],\n",
       " 'nominal_voltages_dont_match': {'trafo': {'hv_lv_swapped': [0]}},\n",
       " 'invalid_values': {'bus': [(5, 'vn_kv', -20.0, '>0')],\n",
       "  'line': [(0, 'length_km', 0.0, '>0')]},\n",
       " 'overload': {'load': False, 'generation': False},\n",
       " 'wrong_switch_configuration': False,\n",
       " 'wrong_reference_system': {'loads': [0]}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# report with warnings_only\n",
    "pp.diagnostic(faulty_net, warnings_only=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "\n",
      "_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: missing_bus_indices:\n",
      "hp.pandapower.diagnostic_reports - INFO: \n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No missing bus indices found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: disconnected_elements:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: disonnected_section: {'buses': [5], 'switches': [7, 8], 'lines': [3], 'loads': [3]}\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: different_voltage_levels_connected:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: lines:\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0: buses [1, 2]\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 1: buses [2, 3]\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 3: buses [4, 5]\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 4: buses [5, 6]\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: impedance_values_close_to_zero:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0: r_ohm <= 0.001 or x_ohm <= 0.001\n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge after replacing implausible elements with switches.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: nominal_voltages_dont_match:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: trafo:\n",
      "hp.pandapower.diagnostic_reports - WARNING: hv_lv_swapped: [0]\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: invalid_values:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: bus:\n",
      "hp.pandapower.diagnostic_reports - WARNING: bus 5: 'vn_kv' = -20.0 (restriction: >0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line:\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0: 'length_km' = 0.0 (restriction: >0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: overload:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Overload check failed: Power flow still does not converge with load and generation scaled down to 0.1 percent.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No buses with multiple gens and/or ext_grids found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: wrong_switch_configuration:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge with all switches closed.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: External grid found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: wrong_reference_system:\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: loads [0]: wrong reference system.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No elements with deviations from std_type found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: numba_comparison failed due to the following error:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Power Flow nr did not converge after 10 iterations!\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No parallel switches found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: _____________ END OF PANDAPOWER DIAGNOSTIC _____________ \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'disconnected_elements': [{'buses': [5],\n",
       "   'switches': [7, 8],\n",
       "   'lines': [3],\n",
       "   'loads': [3]}],\n",
       " 'different_voltage_levels_connected': {'lines': [0, 1, 3, 4]},\n",
       " 'impedance_values_close_to_zero': [{'line': [0]},\n",
       "  {'loadflow_converges_with_switch_replacement': False}],\n",
       " 'nominal_voltages_dont_match': {'trafo': {'hv_lv_swapped': [0]}},\n",
       " 'invalid_values': {'bus': [(5, 'vn_kv', -20.0, '>0')],\n",
       "  'line': [(0, 'length_km', 0.0, '>0')]},\n",
       " 'overload': {'load': False, 'generation': False},\n",
       " 'wrong_switch_configuration': False,\n",
       " 'wrong_reference_system': {'loads': [0]}}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# report with summaries only\n",
    "pp.diagnostic(faulty_net, report_style=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Let's have a look a the default report again"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "\n",
      "_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: Checking for missing bus indices...\n",
      "hp.pandapower.diagnostic_reports - INFO: \n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No missing bus indices found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for elements without a connection to an external grid...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Disconnected section found, consisting of the following elements:\n",
      "hp.pandapower.diagnostic_reports - WARNING: buses: [5]\n",
      "hp.pandapower.diagnostic_reports - WARNING: switches: [7, 8]\n",
      "hp.pandapower.diagnostic_reports - WARNING: lines: [3]\n",
      "hp.pandapower.diagnostic_reports - WARNING: loads: [3]\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 5 disconnected element(s) found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for connections of different voltage levels...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0 connects bus 1: 20 kV bar (vn_kv = 20.0) and bus 2: bus 2 (vn_kv = 30.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 1 connects bus 2: bus 2 (vn_kv = 30.0) and bus 3: bus 3 (vn_kv = 20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 3 connects bus 4: bus 4 (vn_kv = 20.0) and bus 5: bus 5 (vn_kv = -20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: line 4 connects bus 5: bus 5 (vn_kv = -20.0) and bus 6: bus 6 (vn_kv = 20.0)\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 4 element(s) that connect different voltage levels found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for impedance values close to zero...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: line 0: r_ohm <= 0.001 or x_ohm <= 0.001\n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge after replacing implausible elements with switches.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 element(s) with impedance values close to zero found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for components with deviating nominal voltages...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Trafo(s) [0]: hv and lv connectors seem to be swapped\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 component(s) with deviating nominal voltages found\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for invalid_values...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: bus:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Invalid value found: 'bus 5' with attribute 'vn_kv' = -20.0 (data type: <class 'numpy.float64'>). Valid input needs to be >0.\n",
      "hp.pandapower.diagnostic_reports - WARNING: line:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Invalid value found: 'line 0' with attribute 'length_km' = 0.0 (data type: <class 'numpy.float64'>). Valid input needs to be >0.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 2 invalid values found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for overload...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Overload check failed: Power flow still does not converge with load and generation scaled down to 0.1 percent.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No buses with multiple gens and/or ext_grids found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking switch configuration...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge with all switches closed.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: External grid found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for usage of wrong reference system...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Found load 0: 'load 0' with p_mw = -1.0. In load reference system p_mw should be positive.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: Found 1 load(s) with negative p_mw. In load reference system, p_mw should be positive. If the intention was to model a constant generation, please use an sgen instead.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No elements with deviations from std_type found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: numba_comparison failed due to the following error:\n",
      "hp.pandapower.diagnostic_reports - WARNING: Power Flow nr did not converge after 10 iterations!\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No parallel switches found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: _____________ END OF PANDAPOWER DIAGNOSTIC _____________ \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'disconnected_elements': [{'buses': [5],\n",
       "   'switches': [7, 8],\n",
       "   'lines': [3],\n",
       "   'loads': [3]}],\n",
       " 'different_voltage_levels_connected': {'lines': [0, 1, 3, 4]},\n",
       " 'impedance_values_close_to_zero': [{'line': [0]},\n",
       "  {'loadflow_converges_with_switch_replacement': False}],\n",
       " 'nominal_voltages_dont_match': {'trafo': {'hv_lv_swapped': [0]}},\n",
       " 'invalid_values': {'bus': [(5, 'vn_kv', -20.0, '>0')],\n",
       "  'line': [(0, 'length_km', 0.0, '>0')]},\n",
       " 'overload': {'load': False, 'generation': False},\n",
       " 'wrong_switch_configuration': False,\n",
       " 'wrong_reference_system': {'loads': [0]}}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# diagnoses the faulty network\n",
    "pp.diagnostic(faulty_net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Disconnected elements"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "hp.pandapower.diagnostic_reports - WARNING: Checking for elements without a connection to an external grid...\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: Disconnected section found, consisting of the following elements:\n",
    "hp.pandapower.diagnostic_reports - WARNING: buses: [5]\n",
    "hp.pandapower.diagnostic_reports - WARNING: switches: [7, 8]\n",
    "hp.pandapower.diagnostic_reports - WARNING: lines: [3]\n",
    "hp.pandapower.diagnostic_reports - WARNING: loads: [3]\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 5 disconnected element(s) found."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bus 5, line 3, load 3 as well as switches 7 and 8 are not connected to an external grid. To fix that, we close switches 8 and 9."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "faulty_net.switch.closed.loc[8, 9] = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Inconsistent voltages"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "hp.pandapower.diagnostic_reports - WARNING: Checking for connections of different voltage levels...\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: line 0 connects bus 1: 20 kV bar (vn_kv = 20.0) and bus 2: bus 2 (vn_kv = 30.0)\n",
    "hp.pandapower.diagnostic_reports - WARNING: line 1 connects bus 2: bus 2 (vn_kv = 30.0) and bus 3: bus 3 (vn_kv = 20.0)\n",
    "hp.pandapower.diagnostic_reports - WARNING: line 3 connects bus 4: bus 4 (vn_kv = 20.0) and bus 5: bus 5 (vn_kv = -20.0)\n",
    "hp.pandapower.diagnostic_reports - WARNING: line 4 connects bus 5: bus 5 (vn_kv = -20.0) and bus 6: bus 6 (vn_kv = 20.0)\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 4 element(s) that connect different voltage levels found.\n",
    "hp.pandapower.diagnostic_reports - WARNING: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lines 0, 1, 3 and 4 connect different voltage levels. Apparently bus 2 (30 kV) and bus 5 (-20 kV) have wrong voltage levels values. We change that to 20 kV."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "faulty_net.bus.vn_kv.loc[2, 5] = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lines with impedance zero"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "hp.pandapower.diagnostic_reports - WARNING: Checking for impedance values close to zero...\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: line 0: r_ohm <= 0.001 or x_ohm <= 0.001\n",
    "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge after replacing implausible elements with switches.\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 element(s) with impedance values close to zero found.\n",
    "hp.pandapower.diagnostic_reports - WARNING: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Line 0 is 0 km long (and therefore its impedance is 0 ohm). We change the length to 1 km."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "faulty_net.line.length_km.at[0] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Deviating nominal voltages"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "hp.pandapower.diagnostic_reports - WARNING: Checking for components with deviating nominal voltages...\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: Trafo(s) [0]: hv and lv connectors seem to be swapped\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: 1 component(s) with deviating nominal voltages found\n",
    "hp.pandapower.diagnostic_reports - WARNING: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We fix that by swapping the hv and lv connectors of trafo 0 back."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "faulty_net.trafo.hv_bus.at[0] = 0\n",
    "faulty_net.trafo.lv_bus.at[0] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Wrong reference system"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "hp.pandapower.diagnostic_reports - WARNING: Checking for usage of wrong reference system...\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: Found load 0: 'load 0' with p_mw = -1.0. In load reference system p_mw should be positive.\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: SUMMARY: Found 1 load(s) with negative p_mw. In load reference system, p_mw should be positive. If the intention was to model a constant generation, please use an sgen instead."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load 0 has a p_mw value of -1. Since pandapower uses the load reference system for loads, the value should either be positive or an sgen should be used. We assume the former and change the value to 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "faulty_net.load.p_mw.at[0] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "\n",
      "_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: Checking for missing bus indices...\n",
      "hp.pandapower.diagnostic_reports - INFO: \n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No missing bus indices found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No problematic switches found\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No connection of different voltage levels found\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No elements with impedance values close to zero found...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No components with deviating nominal voltages found\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No invalid values found\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: Power flow converges. No overload found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No buses with multiple gens and/or ext_grids found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: Power flow converges. Switch configuration seems ok.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: External grid found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: correct reference system\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No elements with deviations from std_type found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No results with deviations between numba = True vs. False found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - INFO: PASSED: No parallel switches found.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: _____________ END OF PANDAPOWER DIAGNOSTIC _____________ \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# new diagnosis to check, whether we fixed all errors\n",
    "pp.diagnostic(faulty_net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Apparently, all errors have been fixed. So we can try again to run a loadflow calculation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "This pandapower network includes the following parameter tables:\n",
       "   - bus (7 elements)\n",
       "   - load (5 elements)\n",
       "   - switch (12 elements)\n",
       "   - ext_grid (1 element)\n",
       "   - line (6 elements)\n",
       "   - trafo (1 element)\n",
       " and the following results tables:\n",
       "   - res_bus (7 elements)\n",
       "   - res_line (6 elements)\n",
       "   - res_trafo (1 element)\n",
       "   - res_ext_grid (1 element)\n",
       "   - res_load (5 elements)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pp.runpp(faulty_net)\n",
    "faulty_net"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the loadflow calculation runs without problems."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Detecting overloads and wrong switch configurations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Other errors the diagnostic function can help to identify, are networks with too much load or wrong switch configurations. The function automatically checks, if the loadflow converges with all loads and generation scaled down to 0.1% or with all switches closed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# imports the pandapower module\n",
    "import pandapower as pp\n",
    "\n",
    "# defines a function that creates an example network\n",
    "# run this code first in order for the examples to work\n",
    "\n",
    "def overload_example_network():\n",
    "\n",
    "    net = pp.create_empty_network()\n",
    "\n",
    "    pp.create_bus(net, name = \"110 kV bar\", vn_kv = 110, type = 'b')\n",
    "    pp.create_bus(net, name = \"20 kV bar\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 2\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 3\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 4\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 5\", vn_kv = 20, type = 'b')\n",
    "    pp.create_bus(net, name = \"bus 6\", vn_kv = 20, type = 'b')\n",
    "    \n",
    "    pp.create_ext_grid(net, 0, vm_pu = 1)\n",
    "\n",
    "    pp.create_line(net, name = \"line 0\", from_bus = 1, to_bus = 2, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 1\", from_bus = 2, to_bus = 3, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 2\", from_bus = 3, to_bus = 4, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 3\", from_bus = 4, to_bus = 5, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 4\", from_bus = 5, to_bus = 6, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "    pp.create_line(net, name = \"line 5\", from_bus = 6, to_bus = 1, length_km = 1, std_type = \"NAYY 4x150 SE\")\n",
    "\n",
    "    pp.create_transformer_from_parameters(net, hv_bus = 0, lv_bus = 1, i0_percent= 0.038, pfe_kw = 11.6,\n",
    "        vkr_percent = 0.322, sn_mva = 40.0, vn_lv_kv = 22.0,\n",
    "        vn_hv_kv = 110.0, vk_percent = 17.8)\n",
    "\n",
    "    pp.create_load(net, 2, p_mw = 100, q_mvar = 0.2, name = \"load 0\")\n",
    "    pp.create_load(net, 3, p_mw = 100, q_mvar = 0.2, name = \"load 1\")\n",
    "    pp.create_load(net, 4, p_mw = 100, q_mvar = 0.2, name = \"load 2\")\n",
    "    pp.create_load(net, 5, p_mw = 100, q_mvar = 0.2, name = \"load 3\")\n",
    "    pp.create_load(net, 6, p_mw = 100, q_mvar = 0.2, name = \"load 4\")\n",
    "\n",
    "    pp.create_switch(net, bus = 1, element = 0, et = 'l')\n",
    "    pp.create_switch(net, bus = 2, element = 0, et = 'l')\n",
    "    pp.create_switch(net, bus = 2, element = 1, et = 'l')\n",
    "    pp.create_switch(net, bus = 3, element = 1, et = 'l')\n",
    "    pp.create_switch(net, bus = 3, element = 2, et = 'l')\n",
    "    pp.create_switch(net, bus = 4, element = 2, et = 'l')\n",
    "    pp.create_switch(net, bus = 4, element = 3, et = 'l', closed = 0)\n",
    "    pp.create_switch(net, bus = 5, element = 3, et = 'l')\n",
    "    pp.create_switch(net, bus = 5, element = 4, et = 'l')\n",
    "    pp.create_switch(net, bus = 6, element = 4, et = 'l')\n",
    "    pp.create_switch(net, bus = 6, element = 5, et = 'l')\n",
    "    pp.create_switch(net, bus = 1, element = 5, et = 'l')\n",
    "    \n",
    "    return net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "LoadflowNotConverged",
     "evalue": "Power Flow nr did not converge after 10 iterations!",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mLoadflowNotConverged\u001b[0m                      Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-58c163ac9ff5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0moverload_net\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0moverload_example_network\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mpp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrunpp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0moverload_net\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\git_repos\\pandapower\\pandapower\\run.py\u001b[0m in \u001b[0;36mrunpp\u001b[1;34m(net, algorithm, calculate_voltage_angles, init, max_iteration, tolerance_mva, trafo_model, trafo_loading, enforce_q_lims, check_connectivity, voltage_depend_loads, consider_line_temperature, **kwargs)\u001b[0m\n\u001b[0;32m    210\u001b[0m     \u001b[0m_check_bus_index_and_print_warning_if_high\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    211\u001b[0m     \u001b[0m_check_gen_index_and_print_warning_if_high\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 212\u001b[1;33m     \u001b[0m_powerflow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    213\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    214\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\git_repos\\pandapower\\pandapower\\powerflow.py\u001b[0m in \u001b[0;36m_powerflow\u001b[1;34m(net, **kwargs)\u001b[0m\n\u001b[0;32m     80\u001b[0m         \u001b[0m_clean_up\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mres\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     81\u001b[0m         raise LoadflowNotConverged(\"Power Flow {0} did not converge after \"\n\u001b[1;32m---> 82\u001b[1;33m                                    \"{1} iterations!\".format(algorithm, max_iteration))\n\u001b[0m\u001b[0;32m     83\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     84\u001b[0m         \u001b[0mnet\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"_ppc\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mLoadflowNotConverged\u001b[0m: Power Flow nr did not converge after 10 iterations!"
     ]
    }
   ],
   "source": [
    "overload_net = overload_example_network()\n",
    "pp.runpp(overload_net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "\n",
      "_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking for overload...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: overload found: Power flow converges with load scaled down to 0.1 percent.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: Checking switch configuration...\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      "hp.pandapower.diagnostic_reports - WARNING: Power flow still does not converge with all switches closed.\n",
      "hp.pandapower.diagnostic_reports - WARNING: \n",
      " --------\n",
      "\n",
      "hp.pandapower.diagnostic_reports - WARNING: _____________ END OF PANDAPOWER DIAGNOSTIC _____________ \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'overload': {'load': True, 'generation': False},\n",
       " 'wrong_switch_configuration': False}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pp.diagnostic(overload_net, warnings_only = True)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "hp.pandapower.diagnostic_reports - WARNING: Checking for overload...\n",
    "hp.pandapower.diagnostic_reports - WARNING: \n",
    "hp.pandapower.diagnostic_reports - WARNING: overload found: Power flow converges with load scaled down to 0.1 percent.\n",
    "hp.pandapower.diagnostic_reports - WARNING: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, with loads scaled down, the loadflow converges, which helps to isolate the problem."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
